package order

import (
	"context"
	"strconv"
	"time"

	"gitee.com/wenchangshou/simple-admin-pancakebot-rpc/internal/svc"
	"gitee.com/wenchangshou/simple-admin-pancakebot-rpc/internal/utils/dberrorhandler"
	"gitee.com/wenchangshou/simple-admin-pancakebot-rpc/types/pancakebot"
	"github.com/agui-coder/simple-admin-pay-rpc/types/pay"
	"github.com/suyuan32/simple-admin-common/i18n"

	"github.com/zeromicro/go-zero/core/errorx"
	"github.com/zeromicro/go-zero/core/logx"
)

type UpdatePancakebotRefundPaidLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUpdatePancakebotRefundPaidLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdatePancakebotRefundPaidLogic {
	return &UpdatePancakebotRefundPaidLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UpdatePancakebotRefundPaidLogic) UpdatePancakebotRefundPaid(in *pancakebot.UpdatePancakebotRefundPaidReq) (*pancakebot.BaseResp, error) {
	// 1. 查询煎饼订单
	pancakebotOrder, err := l.svcCtx.DB.PancakebotOrder.Get(l.ctx, in.Id)
	if err != nil {
		return nil, dberrorhandler.DefaultEntError(l.Logger, err, in)
	}

	// 2. 验证退款订单号是否匹配(退款订单号不能等于支付订单号)
	if pancakebotOrder.PayOrderId == in.PayRefundId {
		logx.Errorf("[UpdatePancakebotRefundPaid][order(%d) 退款单不匹配(%d)，请进行处理！order 数据是：%+v]",
			in.Id, in.PayRefundId, pancakebotOrder)
		return nil, errorx.NewInvalidArgumentError("PANCAKEBOT_ORDER_REFUND_FAIL_REFUND_ORDER_ID_ERROR")
	}

	// 3. 查询退款订单详情
	refundInfo, err := l.svcCtx.PayRpc.GetRefundById(l.ctx, &pay.IDReq{Id: in.PayRefundId})
	if err != nil {
		return nil, dberrorhandler.DefaultEntError(l.Logger, err, in)
	}

	// 4. 验证退款订单状态是否为成功
	if uint8(*refundInfo.Status) != uint8(pay.PayStatus_PAY_SUCCESS) {
		logx.Errorf("[UpdatePancakebotRefundPaid][order(%d) 退款单状态不是成功，请进行处理！refund 数据是：%+v]",
			in.Id, refundInfo)
		return nil, errorx.NewInvalidArgumentError("PANCAKEBOT_ORDER_REFUND_FAIL_REFUND_NOT_SUCCESS")
	}

	// 5. 验证退款金额是否匹配
	refundPrice := int32(*refundInfo.RefundPrice)
	orderPrice := int32(pancakebotOrder.Price)
	if refundPrice != orderPrice {
		logx.Errorf("[UpdatePancakebotRefundPaid][order(%d) payRefund(%d) 退款金额不匹配，请进行处理！orderPrice=%d, refundPrice=%d]",
			in.Id, in.PayRefundId, orderPrice, refundPrice)
		return nil, errorx.NewInvalidArgumentError("PANCAKEBOT_ORDER_REFUND_FAIL_REFUND_PRICE_NOT_MATCH")
	}

	// 6. 验证商户退款单号是否匹配
	merchantRefundId := strconv.FormatUint(in.Id, 10) + "-refund"
	if *refundInfo.MerchantRefundId != merchantRefundId {
		logx.Errorf("[UpdatePancakebotRefundPaid][order(%d) 退款单商户订单号不匹配(%s)，请进行处理！expected=%s, actual=%s]",
			in.Id, *refundInfo.MerchantRefundId, merchantRefundId, *refundInfo.MerchantRefundId)
		return nil, errorx.NewInvalidArgumentError("PANCAKEBOT_ORDER_REFUND_FAIL_REFUND_ID_NOT_MATCH")
	}

	// 7. 更新订单退款信息
	refundTime := time.UnixMilli(*refundInfo.SuccessTime)
	err = l.svcCtx.DB.PancakebotOrder.UpdateOneID(in.Id).
		SetPayRefundID(in.PayRefundId).
		SetRefundPrice(refundPrice).
		SetNillableRefundTime(&refundTime).
		Exec(l.ctx)

	if err != nil {
		return nil, dberrorhandler.DefaultEntError(l.Logger, err, in)
	}

	return &pancakebot.BaseResp{Msg: i18n.UpdateSuccess}, nil
}
